/**
 * 获取文件权限
*/

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

int main(int argc, char **argv)
{

    if (argc < 2)
    {
        printf("Usage: %s filename", argv[0]);
        exit(EXIT_FAILURE);
    }

    struct stat stat_buf;
    if (lstat(argv[1], &stat_buf) < 0)
    {
        fprintf(stderr, "%s %s\n", argv[1], strerror(errno));
        exit(EXIT_FAILURE);
    }

    int per_user = 0, per_group = 0, per_other = 0;

    if (S_IRUSR & stat_buf.st_mode)
    {
        per_user += 4;
    }
    if (S_IWUSR & stat_buf.st_mode)
    {
        per_user += 2;
    }
    if (S_IXUSR & stat_buf.st_mode)
    {
        per_user += 1;
    }

    if (S_IRGRP & stat_buf.st_mode)
    {
        per_group += 4;
    }
    if (S_IWGRP & stat_buf.st_mode)
    {
        per_group += 2;
    }
    if (S_IXGRP & stat_buf.st_mode)
    {
        per_group += 1;
    }

    if (S_IROTH & stat_buf.st_mode)
    {
        per_other += 4;
    }
    if (S_IWOTH & stat_buf.st_mode)
    {
        per_other += 2;
    }
    if (S_IXOTH & stat_buf.st_mode)
    {
        per_other += 1;
    }

    printf("0%d%d%d\n", per_user, per_group, per_other);

    return 0;
}